package com.dongyun.loraweb.controller;

import java.util.Base64;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.dongyun.loraweb.dao.UserRepository;
import com.dongyun.loraweb.entity.LoraUser;
import com.dongyun.loraweb.json.Grid;
import com.dongyun.loraweb.json.Result;
import com.dongyun.loraweb.util.StrUtil;

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/user")
public class UserController extends AbstractController {
	@Autowired
	private UserRepository userRepository;

	@ApiOperation("添加用户")
	@RequestMapping(value = "/addUser", method = { RequestMethod.POST, RequestMethod.GET })
	public Result addUser(@RequestParam String identifier,@RequestParam String name, String fingerprint) {
		if (StrUtil.isEmpty(identifier) || StrUtil.isEmpty(name)) {
			return createResult("数据不完整！");
		}
		
		LoraUser loraUser = userRepository.findByIdentifier(identifier);
		if (loraUser == null) {
			loraUser=new LoraUser();
		}
		loraUser.setIdentifier(identifier);
		loraUser.setName(name);
		
		if(fingerprint!=null){
			byte[] decode = Base64.getDecoder().decode(fingerprint.getBytes());
			loraUser.setFingerprint(decode);
		}
		
		LoraUser save = userRepository.save(loraUser);
		return createSuccessResult("保存成功！",save.getUuid());
	}
	@ApiOperation("修改用户")
	@RequestMapping(value = "/editUser", method = { RequestMethod.POST, RequestMethod.GET })
	public Result editUser(@RequestParam String identifier,@RequestParam String name,String fingerprint,@RequestParam String uuid) {
		if (StrUtil.isEmpty(uuid)) {
			return createResult("数据不完整！");
		}
		LoraUser loraUser = userRepository.findOne(uuid);
		if(loraUser==null){
			return createResult("用户不存在");
		}
		if (!StrUtil.isEmpty(identifier)) {
			loraUser.setIdentifier(identifier);
		}
		if (!StrUtil.isEmpty(name)) {
			loraUser.setName(name);
		}
		if(fingerprint!=null){
			byte[] decode = Base64.getDecoder().decode(fingerprint.getBytes());
			loraUser.setFingerprint(decode);
		}
		LoraUser save = userRepository.save(loraUser);
		return createResult(true,"保存成功！",save.getUuid());
	}

	@ApiOperation("删除用户")
	@RequestMapping(value = "/deletedUser", method = { RequestMethod.POST, RequestMethod.GET })
	public Result deletedUser(@RequestParam String id) {
		try {
			userRepository.delete(id);
			return createSuccessResult("删除成功！");
		} catch (Exception e) {
			return createResult("删除失败", e.getMessage());
		}
	}

	@ApiOperation("获取用户列表")
	@RequestMapping(value = "/getAllUsers", method = { RequestMethod.POST, RequestMethod.GET })
	public Grid getAllUsers() {
		List<LoraUser> list = userRepository.findAll();
		return createGrid(list.size() * 1l, list);
	}
}
